<?php
/**
 * Created by PhpStorm.
 * User: Sunmer
 * Date: 2019/5/31 0031
 * Time: 11:20
 * Email:Sunmer@foxmail.com
 */

namespace app\api\controller;
use think\Db;

class Register extends BaseController{
    /**
     * 注册
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function register(){
        //接受传递的参数
        $res = request()->param();
        if (empty($res['mobile'])||empty($res['code'])||empty($res['password'])||!isset($res['sub'])){
            return self::ajax_return('null','参数不完整','201');
        }
        //查询手机号和验证码做验证
        $muprem = DB::table('tp_code')->where('member_mobile',$res['mobile'])->find();
        //验证手机号是否匹配---后期更改
        if($muprem['member_mobile'] != $res['mobile']){
            return self::ajax_return('null','手机号不匹配','201');
        }
        //验证验证码是否匹配
        if($muprem['code'] != $res['code']){
            return self::ajax_return('null','验证码不正确','201');
        }
        //查询手机号是否被注册
        $parem = DB::table('tp_user')->where('member_mobile',$res['mobile'])->find();
        if ($parem){
            return self::ajax_return('null','手机号已被注册','201');
        }else{
            $data =[
                'member_id'=>$this->member_id(),
                'member_mobile'=>$res['mobile'],
                'member_password'=>$this->password($res['password']),
                'member_name'=>$res['mobile'],
                'member_status'=>1,
                'create_at'=>date('Y-m-d H:i:s',time()),
            ];
            if($res['sub']){
                $data['member_sub'] = $res['sub'];
            }

           $result = DB::table('tp_user')->insert($data);
            if ($result){
                if(isset($data['member_sub'])){
                    DB::table('tp_user')
                        ->where(['id' => $data['member_sub']])
                        ->setInc('member_sco');
                    $result = DB::table('tp_user')
                        ->where(['id' => $data['member_sub']])
                        ->field(['member_lv','member_mobile'])
                        ->find();
                    $info = [
                        'member_mobile'=>$result['member_mobile'],
                        'member_lv'=>$result['member_lv'],
                        'members_mobile'=>$res['mobile'],
                        'members_lv'=>0,
                    ];
                    Db::table('tp_relation')->insert($info);
                }
                return self::ajax_return($data,'注册成功','200');
            }{
                return self::ajax_return('null','注册失败','201');
            }
        }
    }
    /**
     * 随机生成验证码
     * @param int $len
     * @return string
     */
    public function code($len=4)
    {
        return str_pad(mt_rand(1, pow(10, $len) - 1), $len, STR_PAD_LEFT);
    }

    /**
     * 会员编号
     * @param int $length
     * @return string
     */
    public function member_id($length = 10){
        $chars = 'ABCDEFGHIJKLMNOPQRETWVUXYZ0123456789';
        $member_id = '';
        for ( $i = 0; $i < $length; $i++ )
        {
            $member_id .=$chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        $member_id ='TKWZ'.$member_id;
        return $member_id;
    }
}